package com.zhaoxianglong.priciple;

import org.junit.Test;

/**
 * @program: design-learning
 * @description: 依赖倒转原则
 * @author: zhaoxianglong
 * @create: 2021-03-17 14:27
 **/
public class DependencyInversionTest {

    /**
     * 不使用依赖倒转原则测试
     * 简单,易想到
     * 如果recive使用其他类,需新建类,且需增加新的revice方法
     */
    @Test
    public void test1(){
        Persion persion = new Persion();
        persion.revice(new Email());
    }

    /**
     * 使用依赖倒转原则
     */
    @Test
    public void test2(){
        Persion1 persion1 = new Persion1();
        persion1.receive(new Email1());
        persion1.receive(new WeChat());
    }
}


class Persion{
    public void revice(Email email){
        System.out.println(email.getInfo());
    }
}
class Email{
    public String getInfo(){
        return "hello";
    }
}

interface Ireceive{
    String getInfo();
}

class Persion1{
    public void receive(Ireceive ireceive){
        System.out.println(ireceive.getInfo());
    }
}

class Email1 implements Ireceive{

    @Override
    public String getInfo() {
        return "hello Emial";
    }
}

class WeChat implements Ireceive{

    @Override
    public String getInfo() {
        return "hello wechat";
    }
}